<template>
    <div class="search">
        <!-- 顶部区域 -->
        <Header></Header>

        <!-- 搜索历史记录 -->
        <section>
            <div class="search-history">
                <h2>
                    <i class="iconfont icon-fangdajing"></i>
                    <span>搜索历史</span>
                    <span @click="clearSearchHistory">清空</span>
                </h2>
                <ul v-if="searchArr.length">
                    <li v-for="(item, index) in searchArr" :key="index" @click="searchList(item)">{{ item }}</li>
                </ul>
                <p v-else>暂无搜索记录</p>
            </div>
        </section>
    </div>
</template>

<script setup>
import Header from '@/components/Search/Header.vue';
import { ref,onMounted, onUpdated } from 'vue'
import { showConfirmDialog } from 'vant'
import { useRouter } from 'vue-router'
const router = useRouter()
let searchArr = ref([])
onMounted(() => {
    console.log('onMounted')
    searchArr.value = JSON.parse(localStorage.getItem('searchList')) || []
    console.log(searchArr.value)
})
// 清空搜索历史
const clearSearchHistory = () => {
    // 弹出对话框
    showConfirmDialog({
        title: '清空历史记录',
        message: '是否确认清空搜索历史？',
    })
    .then(() => {
        // 确认
        console.log('清空')
        localStorage.removeItem('searchList') // 把缓存中的数据清空
        searchArr.value = []
    })
    .catch(() => {
        // 取消
        console.log('取消')
    })
}
// 点击历史记录的搜索功能
const searchList = (item) => {
    console.log(item)
    // 路由跳转 到搜索页
    router.push({name: 'list', query: {key: item}})
}

</script>

<style scoped>
.search{
    width: 100vm;
    height: 100vm;
    overflow: hidden;
    background-color: #fff;
    display: flex;
    flex-direction: column;
}
section{
    flex: 1;
    background-color: #fff;
    overflow: hidden;
}
.search-history h2{
    position: relative;
    padding: 0.2rem;
    font-weight: 300;
    font-size: 0.4rem;
}

.search-history h2 span:last-child{
    position: absolute;
    right: 0.4rem;
    font-size: 0.35rem;
}
.search-history ul{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 0 0.26rem;
}
.search-history ul li{
    padding: 0.08rem 0.16rem;
    margin: 0.2rem ;
    font-size: 0.35rem;
    border: 1px solid #ccc;
    border-radius: 0.2rem;
    
}
</style>